package com.ruoyi.web.controller.erpbase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SaleOrPurchType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Appointment;
import com.ruoyi.system.domain.ReturnData;
import com.ruoyi.system.domain.Waybills;
import com.ruoyi.system.service.IWaybillsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约服务
 * 
 * @author he
 * @date 2025-08-18
 */
@Api(tags = "预约接口")
@RestController
@RequestMapping("/appointment")
public class AppointmentController extends BaseController
{
    @Autowired
    private IWaybillsService waybillsService;


    @ApiOperation("微信端注册")
    @Log(title = "微信端注册", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Appointment appointment)
    {
        return toAjax(waybillsService.register(appointment));
    }

    @ApiOperation("微信端注册信息")
    @Log(title = "微信端注册信息查询", businessType = BusinessType.OTHER)
    @PostMapping("/getRegisterInfo")
    public AjaxResult getRegisterInfo(@RequestBody Appointment appointment)
    {
        return success(waybillsService.getRegisterInfo(appointment));
    }


    /**
     * 预约运单
     */
//    @PreAuthorize("@ss.hasPermi('system:waybills:add')")
    @ApiOperation("预约运单")
    @Log(title = "运单", businessType = BusinessType.INSERT)
    @PostMapping("/book")
    public AjaxResult book(@RequestBody Appointment appointment)
    {
        return success(waybillsService.book(appointment));
    }

    @ApiOperation("预约运单确认")
    @Log(title = "运单", businessType = BusinessType.INSERT)
    @PostMapping("/bookConfirm")
    public AjaxResult bookConfirm(@RequestBody Waybills waybills)
    {
        int i = waybillsService.bookConfirm(waybills);
        if (i == 2){
            return error("该车存在已预约但未完成的运单,无法预约");
        } else {
            return toAjax(i);
        }
    }



    /**
     * 获取生成 运单详细信息
     */
    @ApiOperation("当前预约记录")
    @ApiImplicitParam(name = "fwid", value = "微信唯一码", required = true, dataType = "String", example = "123")
    @GetMapping(value = "/waybill")
    public AjaxResult getInfo(@RequestParam("fwid") String code)
    {
        return success(waybillsService.selectWaybillsByCode(code));
    }

    /**
     * 获取生成 预约记录二维码
     */
    @ApiOperation("最新预约记录二维码")
    @ApiImplicitParam(name = "fwid", value = "微信唯一码", required = true, dataType = "String", example = "123")
    @GetMapping(value = "/waybillQrCode")
    public AjaxResult waybillQrCode(@RequestParam("fwid") String code)
    {
        Waybills waybills = waybillsService.selectWaybillsByCode(code);
        if (ObjectUtils.isNotEmpty(waybills)){
            return success((Object) waybills.getSqare03());
        } else {
            return error("该运单不存在");
        }

    }


    @ApiOperation("所有预约记录")
    @PostMapping(value = "/waybillList")
    public AjaxResult getInfoList(@RequestBody Appointment appointment)
    {
        Waybills waybills = new Waybills();
        waybills.setLicensePlate(appointment.getLicensePlate());
        waybills.setFwid(appointment.getFwid());
//        waybills.setType(Long.valueOf(SaleOrPurchType.SALE.getCode()));
        List<Waybills> list = waybillsService.selectWaybillsList(waybills);
        return success(list);
    }



    @ApiOperation("根据预约码查询运单")
    @PostMapping("/waybills")
    public AjaxResult list(@RequestBody Appointment appointment)
    {
        Waybills waybills = new Waybills();
        waybills.setLicensePlate(appointment.getLicensePlate());
//        waybills.setType(Long.valueOf(SaleOrPurchType.SALE.getCode()));
        List<Waybills> list = waybillsService.selectWaybillsList(waybills);
        return success(list);
    }


}
